package com.itdaan.portal.business.solr;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
/**
 * 使用反射给类对象赋值和获取值
 * @author www.itdaan.com
 */
public class ModelUtil {

	//给属性赋值
	public static void setValue(Object model,String name,Object value){
		Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
        try {
            for (int j = 0; j < field.length; j++) { // 遍历所有属性
                String fieldName = field[j].getName(); // 获取属性的名字
                String type = field[j].getGenericType().toString(); // 获取属性的类型
                if(fieldName.equalsIgnoreCase(name)){
                	fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                	 if (type.equals("class java.lang.String")) { 
                		 Method m = model.getClass().getMethod("set" + fieldName,String.class);
                		 if(value != null)
                	     m.invoke(model, value.toString());
                	 }
                	 if(type.equals("class java.lang.Integer")){
                		 Method m = model.getClass().getMethod("set" + fieldName,Integer.class);
                		 if(value != null)
                		 m.invoke(model, Integer.parseInt(value.toString()));
                	 }
                	 if(type.equals("class java.lang.Boolean")){
                		 Method m = model.getClass().getMethod("set" + fieldName,Boolean.class);
                		 if(value != null)
                		 m.invoke(model, (Boolean)value);
                	 }
                	 if(type.equals("class java.lang.Long")){
                		 Method m = model.getClass().getMethod("set" + fieldName,Long.class);
                		 if(value != null)
                		 m.invoke(model, Long.parseLong(value.toString()));
                	 }
                	 if(type.equals("class java.util.Date")){
                		 Method m = model.getClass().getMethod("set" + fieldName,Date.class);
                		 if(value != null){
								m.invoke(model, value);
                		 }
                	 }                	 
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }		
	}
	
	public static Object getValue(Object model,String name){
		Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
        try {
            for (int j = 0; j < field.length; j++) { // 遍历所有属性
                String fieldName = field[j].getName(); // 获取属性的名字
                String type = field[j].getGenericType().toString(); // 获取属性的类型
                if(fieldName.equalsIgnoreCase(name)){
                	 name = name.substring(0, 1).toUpperCase() + name.substring(1);
                	 if (type.equals("class java.lang.String")) { 
                		 Method m = model.getClass().getMethod("get" + name);
                		 return (String)m.invoke(model);
                	 }
                	 if(type.equals("class java.lang.Integer")){
                		 Method m = model.getClass().getMethod("get" + name);
                		 return (Integer)m.invoke(model);
                	 }
                	 if(type.equals("class java.lang.Boolean")){
                		 Method m = model.getClass().getMethod("get" + name);
                		 return (Boolean)m.invoke(model);
                	 }
                	 if(type.equals("class java.lang.Long")){
                		 Method m = model.getClass().getMethod("get" + name);
                		 return (Long)m.invoke(model);
                	 }
                	 if(type.equals("class java.util.Date")){
                		 Method m = model.getClass().getMethod("get" + name);
                		 return (Date)m.invoke(model);
                	 }                	 
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
	}
	
}
